package com.mjk.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 
 * @Description: 数字处理工具类
 */
public class NumberUtil {

	/**
	 * 
	 * 格式化数字，四舍五入小数点后二位数字
	 * 
	 * @param d
	 * @return
	 */
	public static String formatFour(double d) {
		DecimalFormat moneyFormat = new DecimalFormat("#.0000");
		return moneyFormat.format(d);
	}

	/**
	 * 
	 * 格式化数字，四舍五入小数点后二位数字
	 * 
	 * @param d
	 * @return
	 */
	public static String formatTwo(double d) {
		DecimalFormat moneyFormat = new DecimalFormat("#.00");
		return moneyFormat.format(d);
	}

	/**
	 * 
	 * 格式化数字，四舍五入小数点后一位数字
	 * 
	 * @param d
	 * @return
	 */
	public static String formatOne(double d) {
		DecimalFormat moneyFormat = new DecimalFormat("#.0");
		return moneyFormat.format(d);
	}

	/**
	 * 
	 * 格式化数字，不保留小数点
	 * 
	 * @param d
	 * @return
	 */
	public static String format(double d) {
		DecimalFormat moneyFormat = new DecimalFormat("#");
		return moneyFormat.format(d);
	}

	public static void main(String[] args) {
		String a = "5000.78";
		System.out.println(getBigDecimalTwo(a));
	}

	/**
	 * 
	 * 取绝对值
	 * 
	 * @param d
	 * @return
	 */
	public static float abs(float d) {
		return Math.abs(d);
	}

	/**
	 * 
	 * 返回 Long类型数组
	 * 
	 * @param longSet
	 * @return
	 */
	public static Long[] getLongs(Set<Long> longSet) {
		Long[] longArray = null;
		if (null != longSet && longSet.size() > 0) {
			longArray = new Long[longSet.size()];
			int i = 0;
			for (Long l : longSet) {
				longArray[i] = l;
				i++;
			}

		}
		return longArray;
	}

	/**
	 * 
	 * 返回 Long类型数组
	 * 
	 * @param longSet
	 * @return
	 */
	public static Long[] getLongs(List<String> list) {
		Long[] longArray = null;
		if (null != list && list.size() > 0) {
			longArray = new Long[list.size()];
			int i = 0;

			for (String str : list) {
				longArray[i] = NumberUtil.parseLong(str);
				i++;
			}
		}
		return longArray;
	}

	/**
	 * 
	 * 将list集合返回Integer数组
	 * 
	 * @param list
	 * @return
	 */
	public static Integer[] getIntergers(List<String> list) {
		Integer[] integerArray = null;
		if (null != list && list.size() > 0) {
			integerArray = new Integer[list.size()];
			int i = 0;

			for (String str : list) {
				integerArray[i] = NumberUtil.parseInteger(str);
				i++;
			}
		}
		return integerArray;
	}

	/**
	 * 
	 * 判断数组中是否包含param参数
	 * 
	 * @param longSet
	 * @param param
	 * @return
	 */
	public static boolean includeSet(Set<Long> longSet, Long param) {
		boolean boo = false;
		if (null != longSet && longSet.size() > 0) {
			for (long l : longSet) {
				if (l == (long) param) {
					boo = true;
				}
			}
		}
		return boo;
	}

	/**
	 * 
	 * 返回 数组形式的字符串（如：1,2,3,4,5）
	 * 
	 * @param longSet
	 * @return
	 */
	public static String getStringstr(Set<Long> longSet) {
		StringBuffer longArray = new StringBuffer("");
		if (null != longSet && longSet.size() > 0) {
			Iterator<Long> it = longSet.iterator();
			while (it.hasNext()) {
				longArray.append(it.next() + ",");
			}
			longArray.deleteCharAt(longArray.length() - 1);
		}
		return longArray.toString();
	}

	/**
	 * 
	 * 返回 数组形式的字符串（如：1,2,3,4,5）
	 * 
	 * @param longSet
	 * @return
	 */
	public static String getStringBystr(Set<String> StringSet) {
		StringBuffer longArray = new StringBuffer("");
		if (null != StringSet && StringSet.size() > 0) {
			Iterator<String> it = StringSet.iterator();
			while (it.hasNext()) {
				longArray.append("'" + it.next() + "',");
			}
			longArray.deleteCharAt(longArray.length() - 1);
		}
		return longArray.toString();
	}

	/**
	 * 
	 * 将数组返回Set集合
	 * 
	 * @param array
	 * @return
	 */
	public static Set<String> getSet(String[] array) {
		Set<String> set = null;
		if (null != array && array.length > 0) {
			set = new HashSet<String>();
			for (String s : array) {
				set.add(s);
			}

		}
		return set;
	}

	/**
	 * 
	 * 返回 Short类型数组
	 * 
	 * @param shortSet
	 * @return
	 */
	public static Short[] getShorts(Set<Short> shortSet) {
		Short[] shortArray = null;
		if (null != shortSet && shortSet.size() > 0) {
			shortArray = new Short[shortSet.size()];
			int i = 0;
			for (Short s : shortSet) {
				shortArray[i] = s;
				i++;
			}

		}
		return shortArray;
	}

	/**
	 * 
	 * 返回 Integer类型数组
	 * 
	 * @param intergerSet
	 * @return
	 */
	public static Integer[] getIntergers(Set<Integer> intergerSet) {
		Integer[] intergerArray = null;
		if (null != intergerSet && intergerSet.size() > 0) {
			intergerArray = new Integer[intergerSet.size()];
			int i = 0;
			for (Integer x : intergerSet) {
				intergerArray[i] = x;
				i++;
			}

		}
		return intergerArray;
	}

	/**
	 * 
	 * 将字符串转换成Long，如不能转换，则返回null
	 * 
	 * @param num
	 * @return
	 */
	public static Long parseLong(String num) {
		try {
			return Long.parseLong(num);
		} catch (NumberFormatException e) {
			return null;
		}

	}

	/**
	 * 
	 * 将字符串转换成Float，如不能转换，则返回null
	 * 
	 * @param num
	 * @return
	 */
	public static Float parseFloat(String num) {
		try {
			return Float.parseFloat(num);
		} catch (NumberFormatException e) {
			return 0F;
		}

	}

	public static float parseFloatZero(String num) {
		if (StringUtil.isEmpty(num))
			return 0;
		try {
			return Float.parseFloat(num);
		} catch (NumberFormatException e) {
			return 0;
		}

	}

	/**
	 * 
	 * 将字符串转换成Integer，如不能转换，则返回null
	 * 
	 * @param num
	 * @return
	 */
	public static Integer parseInteger(String num) {
		if(StringUtil.isEmpty(num))
		{
			return 0;
		}
		try {
			return Integer.parseInt(num);
		} catch (NumberFormatException e) {
			return 0;
		}

	}
	
	/**
	 * 
	 * 将字符串转换成Integer，如不能转换，则返回null
	 * 
	 * @param num
	 * @return
	 */
	public static Integer parseIntegerZero(String num) {
		if(StringUtil.isEmpty(num))
		{
			return 0;
		}
		try {
			return Integer.parseInt(num);
		} catch (NumberFormatException e) {
			return 0;
		}

	}


	/**
	 * 
	 * 将字符串转换成Double，如不能转换，则返回0
	 * 
	 * @param num
	 * @return
	 */
	public static Double parseDouble(String num) {
		try {
			return Double.parseDouble(num);
		} catch (NumberFormatException e) {
			return 0D;
		}

	}

	/**
	 * 转换为以0开始的字符格式的数字并加1 例：0002
	 * 
	 * @param lenth
	 *            字符长度
	 * @param num
	 *            1
	 * @return
	 */
	public static String createHeadOfZore_int(int length, int num) {
		String result = "";
		int len = (num + "").length();
		for (int i = 0; i < length - len; i++) {
			result += "0";
		}
		result += num;
		return result;
	}

	/**
	 * 转换为以0开始的字符格式的数字并加1 例：0002
	 * 
	 * @param lenth
	 *            字符长度
	 * @param num
	 *            1
	 * @return
	 */
	public static String createHeadOfZore_str(int lenth, int num) {
		String result = "";
		int length = ((++num)+"").length();
		for (int i = length; i < lenth; i++) {
			result += "0";
		}
		result += num;
		return result;
	}
	/**
	 * 
	 * 将字符串是否能转换成Double，如不能转换，则返回false
	 * 
	 * @param num
	 * @return
	 */
	public static boolean isDouble(String num) {
		try {
			Double.parseDouble(num);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	/**
	 * 直接舍弃 保留两位小数
	 * 
	 * @param num
	 * @return
	 */
	public static BigDecimal getBigDecimalTwo(String num) {
		if (StringUtil.isEmpty(num)) {
			num = "0";
		}
		return new BigDecimal(num).setScale(2, BigDecimal.ROUND_DOWN);

	}
}
